// index.js
// 获取应用实例
const { HTTP } = require('../../utils/http');
const app = getApp()
const { api } = require('../../utils/api');
const { fromNowFormat } = require('../../utils/filter');
const { request } = new HTTP();

Page({
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    user: {
      img: '../../images/banner/3.jpeg',
      name: '官方运营小妖',
      job: '93年. 运营.杭州人.现居深圳'
    },

    detail: {},
    focus: false,
    id: '',
    content: '',
    commentList: [],
    userLike: {},

    isShare: false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow(options){
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1    // 根据tab的索引值设置
      }) 
    }
  },
  onLoad(options){
    console.log('e--options--')
    console.log(options)
    wx.showShareMenu({
      // 要求小程序返回分享目标信息
      withShareTicket: true
    }); 
    this.setData({
      id: options.id
    })
    if(options.isShare == 1){
      this.setData({isShare: true})
    } 
    this._showDetail(options.id)
    this._commentQuery(options.id)
    this._themetlikeList(this.data.id)
  },
  getUserInfo(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  onChange() {
    
  },
  onClickLeft() {
    if(this.data.isShare === false) {
      wx.navigateBack({
        delta: 1
      });
    }
    if(this.data.isShare === true) {
      wx.switchTab({
        url: "../square/index"
      })
    }

  },
  // 查询详情
  async _showDetail(id) {
    let {returnCode = '', bean = {}} = await request({
      url: `/api/theme/${id}`,
      method: 'get',
      data: {}
    });
    console.log('--bean--')
    console.log(bean)
    bean.fromNowFormatTime = fromNowFormat(bean.create_time);
    this.setData({
      detail: bean
    })
  },
  //查询评论 
  async _commentQuery(id) {
    let {returnCode = '', beans = []} = await request({
      url: api.commentQuery + '/' + id,
      method: 'get',
      data: {id}
    });
    console.log('--_commentQuery--')
    console.log(beans)
    // console.log(bean)
    beans.item.map(item => {
      item.fromNowFormatTime = fromNowFormat(item.create_time);
      return item;
    });
    this.setData({
      commentList: beans.item
    })
  },
  // 添加评论
  async _commentAdd() {
    let params = {
      theme_id: this.data.id,
      content: this.data.content
    }
    let {returnCode = '', beans = {}} = await request({
      url: api.commentAdd,
      method: 'post',
      data: params
    });
    console.log('--_commentQuery--')
    console.log(beans)
    if(returnCode == '0000'){
      wx.showToast({
        title: '评论成功',
        icon: 'success',
        duration: 2000
      })
      this.setData({
        content: '',
        focus: false
      })
      this._showDetail(this.data.id)
      this._commentQuery(this.data.id)
    }
    // console.log(bean)
    // this.setData({
    //   detail: bean
    // })
  },

   // 点赞评论
   async _commentLike(id) {
    let params = {
      theme_id: parseInt(this.data.id),
      them_comment_id: id
    }
    let {returnCode = '', beans = {}} = await request({
      url: api.commentLike,
      method: 'post',
      data: params
    });
    console.log('--_commentQuery--')
    console.log(beans)
    if(returnCode == '0000'){
      wx.showToast({
        title: '点赞成功',
        icon: 'success',
        duration: 2000
      })
      // this._showDetail(this.data.id)
      this._commentQuery(this.data.id)
    }
  },
  // 取消点赞评论
  async _commentDislike(id) {
    let params = {
      // theme_id: parseInt(this.data.id),
      id: id
    }
    let {returnCode = '', beans = {}} = await request({
      url: api.commentDislike,
      method: 'delete',
      data: params
    });
    if(returnCode == '0000'){
      wx.showToast({
        title: '取消点赞',
        icon: 'success',
        duration: 2000
      })
      // this._showDetail(this.data.id)
      this._commentQuery(this.data.id)
    }
  },
  // 话题喜欢信息列表
  async _themetlikeList(id) {
    let {returnCode = '', beans = {}} = await request({
      url: `/api/theme/${id}/like`,
      method: 'get',
      data: {}
    });
    if(returnCode == '0000') {
      // this.userLike = beans
      this.setData({
        userLike: beans
      })
    }

  },
  // 话题喜欢信息
  async _themetlike(id, isLike) {
    let {returnCode = '', beans = {}} = await request({
      url: `/api/like/theme/${id}`,
      method: 'get',
      data: {}
    });
    if(returnCode == '0000'){
      if(isLike) {
        wx.showToast({
          title: '取消点赞',
          icon: 'success',
          duration: 2000
        })
      }else{
        wx.showToast({
          title: '点赞成功',
          icon: 'success',
          duration: 2000
        })
      }
      this._showDetail(this.data.id)
      this._themetlikeList(this.data.id)
      // this._commentQuery(this.data.id)
    }


  },

  // /api/theme/:id/like
  // 详情点赞
  clickLike(otps) {
    // this._themeDislike()
    this._themetlike(this.data.id, otps.target.dataset.id)
   
  },
  // 评论点赞
  clickCommentLike(otps) {
    console.log('----otps---')
    console.log(otps)
    this._commentLike(otps.target.dataset.id)
  },
  // 取消评论点赞
  cancelClickCommentLike(otps) {
    this._commentDislike(otps.target.dataset.id)
  },
// 聚焦
  clickFocus() {
    console.log('--clickFocus')
    this.setData({
      focus: true
    })
  },
  clickBlur() {
    this.setData({
      focus: false
    })
  },
  getInputValue(e){
    console.log(e)
    this.setData({content: e.detail.value})
  },
  clickSend() {
    console.log('--clickSend-')
    console.log(this.data.content)
    if(!this.data.content) {
      wx.showToast({
        title: '请填写评论',
        icon: 'warning',
        duration: 2000
      })
      
      return
    }
    this._commentAdd()
  },
  

  onShareAppMessage: function (res) {
    console.log('---res---')
    console.log(res)
    return {
      title: "快来看，给你抢了一个好东西！",
      path: "pages/squareDetail/index?id=" + res.target.dataset.id + '&isShare=1'
    }
  }

})
